#include <stdio.h>
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <sys/mman.h>

unsigned char buf_4line[]=
{
    0x00, 0x00, 0x01,
    0x00, 0x01, 0x1E,
    0x00, 0x02, 0x04,
    0x00, 0x03, 0x00,
    0x00, 0x04, 0x00,
    0x00, 0x05, 0x9C,
    0x00, 0x06, 0x00,
    0x00, 0x07, 0x68,
    0x00, 0x08, 0x01,
    0x00, 0x09, 0x19,
    0x00, 0x0A, 0x04,
    0x00, 0x0B, 0x2B,
    0x00, 0x0C, 0x01,
    0x00, 0x0D, 0x02,
    0x00, 0x0E, 0x05,
    0x00, 0x0F, 0x01,
    0x00, 0x10, 0x10,
    0x00, 0x11, 0x9A,
    0x00, 0x12, 0xFF,
    0x00, 0x13, 0xFF,
    0x00, 0x14, 0x3F,
    0x00, 0x15, 0x5D,
    0x00, 0x16, 0x3E,
    0x00, 0x17, 0x9A,
    0x00, 0x18, 0xFF,
    0x00, 0x19, 0xFF,
    0x00, 0x1A, 0x03,
    0x00, 0x1B, 0x3C,
    0x00, 0x1C, 0x03,
    0x00, 0x1D, 0x2A,
    0x00, 0x1E, 0x42,
    0x00, 0x1F, 0x98,
    0x00, 0x20, 0xFF,
    0x00, 0x21, 0xFF,
    0x00, 0x22, 0xFF,
    0x00, 0x23, 0xFF,
    0x00, 0x24, 0x07,
    0x00, 0x25, 0x40,
    0x00, 0x26, 0x4A,
    0x00, 0x27, 0xA0,
    0x00, 0x28, 0x0A,
    0x00, 0x29, 0x11,
    0x00, 0x2A, 0x1B,
    0x00, 0x2B, 0x3F,
    0x00, 0x2C, 0x55,
    0x00, 0x2D, 0xA0,
    0x00, 0x2E, 0x1A,
    0x00, 0x2F, 0x1B,
    0x00, 0x30, 0x01,
    0x00, 0x31, 0x04,
    0x00, 0x32, 0x42,
    0x00, 0x33, 0x45,
    0x00, 0x34, 0x0A,
    0x00, 0x35, 0x41,
    0x00, 0x36, 0x4C,
    0x00, 0x37, 0xA1,
    0x00, 0x38, 0x1B,
    0x00, 0x39, 0x3F,
    0x00, 0x3A, 0x55,
    0x00, 0x3B, 0xA0,
    0x00, 0x3C, 0x13,
    0x00, 0x3D, 0xA0,
    0x00, 0x3E, 0xFF,
    0x00, 0x3F, 0xFF,
    0x00, 0x40, 0x02,
    0x00, 0x41, 0x07,
    0x00, 0x42, 0x46,
    0x00, 0x43, 0x4B,
    0x00, 0x44, 0x3E,
    0x00, 0x45, 0x3F,
    0x00, 0x46, 0x9F,
    0x00, 0x47, 0xA0,
    0x00, 0x48, 0x13,
    0x00, 0x49, 0x1B,
    0x00, 0x4A, 0x3F,
    0x00, 0x4B, 0x55,
    0x00, 0x4C, 0xFF,
    0x00, 0x4D, 0xFF,
    0x00, 0x4E, 0x05,
    0x00, 0x4F, 0xFF,
    0x00, 0x50, 0xFF,
    0x00, 0x51, 0xFF,
    0x00, 0x52, 0xFF,
    0x00, 0x53, 0xFF,
    0x00, 0x54, 0x0D,
    0x00, 0x55, 0x00,
    0x00, 0x56, 0xFF,
    0x00, 0x57, 0x4F,
    0x00, 0x58, 0x83,
    0x00, 0x59, 0x0D,
    0x00, 0x5A, 0x08,
    0x00, 0x5B, 0x00,
    0x00, 0x5C, 0x00,
    0x00, 0x5D, 0x00,
    0x00, 0x5E, 0x00,
    0x00, 0x5F, 0x00,
    0x04, 0x00, 0x01,
    0x04, 0x01, 0x65,
    0x04, 0x02, 0x01,
    0x04, 0x03, 0x00,
    0x04, 0x04, 0x00,
    0x04, 0x05, 0x01,
    0x04, 0x06, 0x01,
    0x04, 0x07, 0x00,
    0x04, 0x08, 0x00,
    0x04, 0x0A, 0x00,
    0x04, 0x0B, 0x01,
    0x04, 0x0C, 0x00,
    0x04, 0x0D, 0x00,
    0x04, 0x16, 0x00,
    0x04, 0x17, 0x00,
    0x04, 0x18, 0x00,
    0x04, 0x19, 0x00,
    0x04, 0x1A, 0x00,
    0x04, 0x1B, 0x00,
    0x04, 0x1C, 0x00,
    0x04, 0x1D, 0x00,
    0x04, 0x1E, 0x00,
    0x04, 0x1F, 0x00,
    0x04, 0x20, 0x06,
    0x04, 0x21, 0x06,
    0x04, 0x22, 0x06,
    0x04, 0x23, 0x06,
    0x04, 0x24, 0x06,
    0x04, 0x25, 0x0A,
    0x04, 0x26, 0x00,
    0x04, 0x27, 0x0A,
    0x04, 0x28, 0x00,
    0x04, 0x29, 0x0A,
    0x04, 0x2A, 0x00,
    0x04, 0x2B, 0x0A,
    0x04, 0x2C, 0x00,
    0x04, 0x2D, 0x0A,
    0x04, 0x2E, 0x00,
    0x04, 0x2F, 0x00,
    0x04, 0x30, 0x00,
    0x04, 0x31, 0x01,
    0x04, 0x32, 0x01,
    0x04, 0x33, 0x04,
    0x04, 0x34, 0x04,
    0x04, 0x35, 0x04,
    0x04, 0x36, 0x04,
    0x04, 0x37, 0x04,
    0x04, 0x38, 0x01,
    0x04, 0x39, 0x01,
    0x04, 0x3A, 0x01,
    0x04, 0x3B, 0x01,
    0x04, 0x3C, 0x01,
    0x04, 0x3D, 0x00,
    0x04, 0x3E, 0x00,
    0x04, 0x3F, 0x0A,
    0x04, 0x40, 0x00,
    0x04, 0x41, 0x0A,
    0x04, 0x42, 0x00,
    0x04, 0x43, 0x0A,
    0x04, 0x44, 0x00,
    0x04, 0x45, 0x0A,
    0x04, 0x46, 0x00,
    0x04, 0x47, 0x0A,
    0x04, 0x48, 0x00,
    0x04, 0x49, 0x28,
    0x04, 0x4A, 0x00,
    0x04, 0x4B, 0x00,
    0x04, 0x4C, 0x00,
    0x04, 0x4D, 0x00,
    0x04, 0x4E, 0x00,
    0x04, 0x4F, 0x00,
    0x04, 0x50, 0x00,
    0x04, 0x51, 0x00,
    0x04, 0x52, 0x00,
    0x04, 0x53, 0x00,
    0x04, 0x54, 0x00,
    0x04, 0x55, 0x00,
    0x04, 0x56, 0x00,
    0x04, 0x57, 0x00,
    0x04, 0x58, 0x00,
    0x04, 0x59, 0x00,
    0x04, 0x5A, 0x00,
    0x04, 0x5B, 0x00,
    0x04, 0x5C, 0x00,
    0x04, 0x5D, 0x00,
    0x04, 0x5E, 0x00,
    0x04, 0x5F, 0x00,
    0x04, 0x60, 0x00,
    0x04, 0x61, 0x00,
    0x04, 0x62, 0x00,
    0x04, 0x63, 0x00,
    0x04, 0x64, 0x99,
    0x04, 0x65, 0x07,
    0x04, 0x66, 0x00,
    0x04, 0x67, 0x14,
    0x04, 0x68, 0x00,
    0x04, 0x69, 0x14,
    0x04, 0x6A, 0x00,
    0x04, 0x6B, 0x14,
    0x04, 0x6C, 0x00,
    0x04, 0x6D, 0x14,
    0x04, 0x6E, 0x00,
    0x04, 0x6F, 0x14,
    0x04, 0x70, 0x00,
    0x04, 0x71, 0x01,
    0x04, 0x72, 0x00,
    0x04, 0x73, 0x00,
    0x04, 0x74, 0x06,
    0x04, 0x75, 0x01,
    0x04, 0x76, 0x00,
    0x04, 0x77, 0x00,
    0x04, 0x78, 0x01,
    0x08, 0x00, 0x90,
    0x08, 0x01, 0x8C,
    0x08, 0x02, 0xC7,
    0x08, 0x03, 0xF3,
    0x08, 0x04, 0x51,
    0x08, 0x05, 0xA9,
    0x08, 0x06, 0xC8,
    0x08, 0x07, 0x42,
    0x08, 0x08, 0xCC,
    0x08, 0x09, 0x50,
    0x08, 0x0A, 0xD4,
    0x08, 0x0B, 0x54,
    0x08, 0x0C, 0xD4,
    0x08, 0x0D, 0x08,
    0x08, 0x0E, 0xA0,
    0x08, 0x0F, 0xDE,
    0x08, 0x10, 0x74,
    0x08, 0x11, 0xB0,
    0x08, 0x12, 0x52,
    0x08, 0x13, 0x59,
    0x08, 0x14, 0x00,
    0x0C, 0x00, 0x2E,
    0x0C, 0x01, 0xC0,
    0x0C, 0x02, 0x51,
    0x0C, 0x03, 0x81,
    0x0C, 0x04, 0x88,
    0x0C, 0x05, 0xA1,
    0x0C, 0x06, 0x03,
    0x10, 0x00, 0x6F,
    0x10, 0x01, 0x18,
    0x10, 0x02, 0x68,
    0x10, 0x03, 0x41,
    0x14, 0x00, 0x00,
    0x14, 0x01, 0x12,
    0x14, 0x02, 0x00,
    0x14, 0x03, 0x04,
    0x14, 0x04, 0x34,
    0x14, 0x05, 0x08,
    0x14, 0x06, 0x06,
    0x14, 0x07, 0x00
};

unsigned char buf2[]={0x8C,0x00,0x3E};

void set_y_rev(unsigned char flip_v)
{
    int fd = open("/dev/spidev1.0", O_RDWR);
    if(flip_v == 0){
        unsigned char flip[]={0x80,0x02,0x04};
        write(fd, flip, 3);
    }
    else{
        unsigned char flip[]={0x80,0x02,0x05};
        write(fd, flip, 3);
    }
    close(fd);
}

void gl7004_init()
{
    int fd;
    int len,i;
    fd = open("/dev/spidev1.0", O_RDWR);
    unsigned char* buf = buf_4line;
    for(i=0;i<248;i++){
	*(buf+i*3)=*(buf+i*3)+0x80;
    	write(fd, buf+i*3, 3);
    }
    usleep(2000);
    write(fd, buf2, 3);
    printf("GL7004 Init\n");
    close(fd);
    usleep(2000);
}